package com.fanketly.musicdiary.ui.components

import android.content.ContentValues
import android.net.Uri
import android.os.Environment
import android.provider.MediaStore
import android.util.Log
import androidx.activity.compose.BackHandler
import androidx.activity.compose.ManagedActivityResultLauncher
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.scaleIn
import androidx.compose.animation.scaleOut
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.AddAPhoto
import androidx.compose.material.icons.filled.AddPhotoAlternate
import androidx.compose.material.icons.filled.InsertPhoto
import androidx.compose.runtime.Composable
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import com.fanketly.musicdiary.R
import com.fanketly.musicdiary.domain.DiaryViewModel
import com.fanketly.musicdiary.domain.MusicViewModel
import com.fanketly.musicdiary.ui.screen.MusicScaffold
import com.fanketly.musicdiary.ui.theme.*
import com.google.accompanist.pager.HorizontalPager
import kotlinx.coroutines.launch

@OptIn(ExperimentalMaterialApi::class)
@Composable
fun SheetLayout(
    sheetState: ModalBottomSheetState,
    openSelectPhotoLauncher: ManagedActivityResultLauncher<String, Uri?>,
    openCameraLauncher: ManagedActivityResultLauncher<Uri, Boolean>,
    viewModel: DiaryViewModel = hiltViewModel(),
    musicViewModel: MusicViewModel = hiltViewModel(),
){}
//{
//
//    val coroutineScope = rememberCoroutineScope()
//    ModalBottomSheetLayout(sheetState = sheetState, sheetContent = {
//        ChooseOrTakePhotoSheet(textColor = TextWhite, {
//            coroutineScope.launch {
//                sheetState.hide()
//            }
//            //start file selection
//            openSelectPhotoLauncher.launch("image/*")
//        }, {
//            coroutineScope.launch {
//                sheetState.hide()
//            }
//            //Build Uri of Save
////            mCameraUri = context.contentResolver.insert(
////                if (Environment.getExternalStorageState() == Environment.MEDIA_MOUNTED)
////                    MediaStore.Images.Media.EXTERNAL_CONTENT_URI else
////                    MediaStore.Images.Media.INTERNAL_CONTENT_URI, ContentValues()
////            )
////            //start taking pictures
////            openCameraLauncher.launch(mCameraUri)
//        })
//    }, content = {
//        AdaptScaffold(backgroundColor = BlackBackgroundColor,
//            contentColor = BlackBackgroundColor,
//            floatingActionButton = {
//                /**
//                 *底部浮动按钮Ui，根据不同状态显示的内容会改变
//                 **/
//                MusicImageAndProgress(
//                    state.currentPage,
//                    isTodayAdded,
//                    musicViewModel,
//                    onClick = viewModel::showToadyDiaryDetail
//                )
//
//            },
//            // floatingActionButton是否应该与bottomBar重叠半个高度
//            isFloatingActionButtonDocked = true,
//            floatingActionButtonPosition = FabPosition.End,
//            bottomBar = {
//                /**
//                 *底部导航Ui
//                 **/
//                MyBottomNavigation(Modifier.navigationBarsPadding(), selectedIndex) {
//                    //改变页数
//                    coroutineScope.launch {
//                        selectedIndex = it
//                        state.animateScrollToPage(it)
//                    }
//                }
//            },
//            content = {
//                HorizontalPager(
//                    count = 3,
//                    state = state,
//                    modifier = Modifier
//                        .statusBarsPadding()
//                        .padding(it)
//                ) { page ->
//                    viewModel.updateDate()
//                    when (page) {
//                        0 -> DiaryCalendar(diaryList, viewModel)//日历Ui
//                        1 -> DiaryList(diaryList, onClick = viewModel::showDiaryListDetail)//日记列表Ui
//                        2 -> MusicScaffold(musicViewModel)//音乐列表
//                    }
//                }
//            })
//        /**
//         *日记详细信息，显示和退出缩放动画
//         **/
//        AnimatedVisibility(
//            visible = viewModel.isShowDiaryDetail,
//            enter = scaleIn(),
//            exit = scaleOut()
//        ) {
//            viewModel.diaryDetailData?.let { model ->
//                imagePath.value = model.imgPath
//                BackHandler(enabled = !sheetState.isVisible, onBack = viewModel::dismissDiaryDetail)
//                DiaryDetail(
//                    model, imagePath,
//                    onUpdateClick = viewModel::update,
//                    onImageClick = { coroutineScope.launch { sheetState.show() } },
//                    onBackClick = viewModel::dismissDiaryDetail
//                )
//            }
//        }
//    })
//}


/**
 * Bottom Popup For Selecting Photo Or Choose Photo
 * */
@Composable
fun ChooseOrTakePhotoSheet(
    textColor: Color = Color.Unspecified,
    onTakeClick: () -> Unit,
    onChooseClick: () -> Unit
) {
    Surface(
        shape = RoundedCornerShape(topStart = 20.dp, topEnd = 20.dp),
        modifier = Modifier
            .navigationBarsPadding()
            .height(150.dp)
    ) {
        Column(
            Modifier
                .padding(SmallDimen)
        ) {
            Row(
                Modifier
                    .fillMaxWidth()
                    .height(70.dp)
                    .clickable {
                        onTakeClick.invoke()
                    },
                verticalAlignment = Alignment.CenterVertically
            ) {
                Icon(
                    imageVector = Icons.Filled.AddAPhoto,
                    contentDescription = stringResource(id = R.string.description_take_photo),
                    modifier = Modifier.size(40.dp), tint = ImageColor
                )
                Text(
                    text = stringResource(id = R.string.description_take_photo),
                    color = textColor,
                    modifier = Modifier.padding(start = MediumDimen)
                )
            }
            Row(
                Modifier
                    .fillMaxWidth()
                    .height(70.dp)
                    .clickable {
                        onChooseClick.invoke()
                    },
                verticalAlignment = Alignment.CenterVertically
            ) {
                Icon(
                    imageVector = Icons.Filled.InsertPhoto,
                    contentDescription = stringResource(id = R.string.description_choose_photo),
                    modifier = Modifier.size(40.dp), tint = ImageColor
                )
                Text(
                    text = stringResource(id = R.string.description_choose_photo),
                    color = textColor,
                    modifier = Modifier.padding(start = MediumDimen)
                )
            }
        }
    }
}